/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package educar.abmCursa;

import educar.controllers.PrincipalController;
import educar.db.JPA;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Matias
 */
public class CursaController implements ActionListener,MouseListener, WindowListener {
            
    private PrincipalController principalController;
    private CursaView cursaView;

// <editor-fold defaultstate="collapsed" desc="Constructor">
    public CursaController(PrincipalController principalControl){ 
        principalController = principalControl;
        cursaView = new CursaView();
        cursaView.setActionListener(this);
        cursaView.setMouseListener(this);
        cursaView.setWindowListener(this);
    }
 // </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Action setView">    
    public void setView() {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = 0;
        int y = 50;
        cursaView.setBounds(x, y, screen.width, (screen.height)-90);
        cursaView.setModeloTabla(this.listarCursa());
        cursaView.setVisible(true);
    }
// </editor-fold>    

// <editor-fold defaultstate="collapsed" desc="Metodos ActionPermormed">    
    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (cursaView.getJbAgregar() == source) {
                AgregarCursaController agregarCursaController = new AgregarCursaController(this);
                agregarCursaController.setView();
        }
        
        if (cursaView.getJbEliminar() == source) {
            EliminarCursaController eliminarCursaController = new EliminarCursaController();
            eliminarCursaController.eliminarRegistro(cursaView.getModeloTabla());
        }    
        
        if (cursaView.getJbBuscar() == source) {
            cursaView.setModeloTabla(null);
            if (cursaView.getCriterios()[0].isEmpty() && cursaView.getCriterios()[1].isEmpty()){
                cursaView.setModeloTabla(listarCursa());
            }
            if (!(cursaView.getCriterios()[0].isEmpty()) && cursaView.getCriterios()[1].isEmpty()){
                cursaView.setModeloTabla(buscarCursaByColumn("codMateria", cursaView.getCriterios()[0]));
            }
            if (cursaView.getCriterios()[0].isEmpty() && !(cursaView.getCriterios()[1].isEmpty())){
                cursaView.setModeloTabla(buscarCursaByColumn("nroAlumno", cursaView.getCriterios()[1]));
            }
            if (!(cursaView.getCriterios()[0].isEmpty()) && !(cursaView.getCriterios()[1].isEmpty())){
                cursaView.setModeloTabla(buscarCursaByKeys(cursaView.getCriterios()));
            }
        }       
    }
    
    @Override
    public void mouseClicked(MouseEvent e) {
        Object source = e.getSource();
        if ((cursaView.getJtCursa() == source) && (e.getClickCount()==2)) {
            ModificarCursaController modificarCursaController = new ModificarCursaController(this);
            int fila = cursaView.getJtCursa().getSelectedRow();
            Vector registro = (Vector) (cursaView.getModeloTabla().getDataVector()).elementAt(fila);
            modificarCursaController.setView(registro, fila);
        }    
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Metodos auxiliares usados">    
    public void agregarATabla(String[] item){
        Object[] vector = new Object[3];
        vector[0] = (boolean) false;
        for (int i = 1; i < 3; i++){
            vector[i] = item[i-1];
        }
        cursaView.getModeloTabla().addRow(vector);
    }

    public void actualizarFila(String[] item, int fila){
        cursaView.getModeloTabla().setValueAt(false, fila, 0);
        for (int i = 1; i < 3; i++){
            cursaView.getModeloTabla().setValueAt(item[i-1], fila, i);
        }
    }
    
    public DefaultTableModel listarCursa() {
            JPA jpa = new JPA();
            Object[] vector = new Object[3];
            DefaultTableModel tabla = (DefaultTableModel) cursaView.getJtCursa().getModel(); 
            ResultSet res = jpa.showAll("cursa");
            try {
                while (res.next()) {
                    vector[0] = false;
                    vector[1] = res.getObject(1);
                    vector[2] = res.getObject(2);                    
                    tabla.addRow(vector);
                }
            } catch (SQLException ex) {
            ex.printStackTrace();
            }
            return tabla;
    }

    public DefaultTableModel buscarCursaByColumn(String column, String criterio) {
        JPA jpa = new JPA();
        Object[] vector = new Object[3];
        DefaultTableModel tabla = (DefaultTableModel) cursaView.getModeloTabla(); 
        ResultSet res = jpa.getByField("cursa", column, criterio);
        try {
            while (res.next()) {
                vector[0] = false;
                vector[1] = res.getObject(1);
                vector[2] = res.getObject(2);                 
                tabla.addRow(vector);
            }
         } catch (SQLException ex) {
            ex.printStackTrace();
         }
         return tabla;       
    }
        
    public DefaultTableModel buscarCursaByKeys(String[] keysCriterio) {
        JPA jpa = new JPA();
        Object[] vector = new Object[3];
        DefaultTableModel tabla = (DefaultTableModel) cursaView.getModeloTabla();
        String[] keys = {"CodMateria", "nroAlumno"};
        ResultSet res = jpa.getByKeys("cursa", keys, keysCriterio);
        try {
            while (res.next()) {
                vector[0] = false;
                vector[1] = res.getObject(1);
                vector[2] = res.getObject(2);                                    
                tabla.addRow(vector);
            }
         } catch (SQLException ex) {
            ex.printStackTrace();
         }
         return tabla;        
    }
        
    @Override
    public void windowClosing(WindowEvent e) {
            principalController.setView();
    }
    
// </editor-fold>    
    
// <editor-fold defaultstate="collapsed" desc="Metodos ActionPerformed no usadas"> 
    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    @Override
    public void windowOpened(WindowEvent e) {

    }

    @Override
    public void windowClosed(WindowEvent e) {

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }
// </editor-fold>    
    
}